﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ASMS.Excel2DbTool.Utils
{
    public class WaiyunStorage
    {
        public static void Run(string filePath)
        {
            DataTable dataTable = Sunboxsoft.Excel.ReadTool.ReadExcel(filePath, true);

            StringBuilder nvBuilder = new StringBuilder("var data = [");
            StringBuilder geoBuilder = new StringBuilder("var geoCoordMap = {");
            bool isFirst = true;
            List<string> names = new List<string>();
            double maxValue = 0;
            for (int index = 0; index < dataTable.Rows.Count; index++)
            {
                string lnglat = Convert.ToString(dataTable.Rows[index][4]);
                lnglat = lnglat.Replace("北纬", string.Empty)
                    .Replace("东经", string.Empty)
                    .Replace("度", string.Empty)
                    .Replace("，", ",")
                    .Replace("；", ",");

                string[] lnglatArr = lnglat.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                if (lnglatArr.Length != 2)
                {
                    Console.WriteLine(string.Format("{0}格式不正确", lnglat));
                    continue;
                }
                string name = Convert.ToString(dataTable.Rows[index][2]);
                if (names.Contains(name))
                {
                    Console.WriteLine(string.Format("{0}名称已存在", name));
                    continue;
                }
                names.Add(name);

                double value = 0;
                if (double.TryParse(Convert.ToString(dataTable.Rows[index][8]), out value) == false)
                {
                    Console.WriteLine(string.Format("值{0}格式不正确", Convert.ToString(dataTable.Rows[index][8])));
                    continue;
                }
                if (value > maxValue)
                {
                    maxValue = value;
                }
            }

            names.Clear();
            for (int index = 0; index < dataTable.Rows.Count; index++)
            {
                string lnglat = Convert.ToString(dataTable.Rows[index][4]);
                lnglat = lnglat.Replace("北纬", string.Empty)
                    .Replace("东经", string.Empty)
                    .Replace("度", string.Empty)
                    .Replace("，", ",")
                    .Replace("；", ",");
                double value = 0;
                if (double.TryParse(Convert.ToString(dataTable.Rows[index][8]), out value) == false)
                {
                    continue;
                }
                if (value > maxValue)
                {
                    maxValue = value;
                }

                string name = Convert.ToString(dataTable.Rows[index][2]);
                if (names.Contains(name))
                {
                    continue;
                }
                names.Add(name);
                string[] lnglatArr = lnglat.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                if (lnglatArr.Length == 2)
                {
                    Console.WriteLine(string.Format("{0},{1}", lnglatArr[0], lnglatArr[1]));
                    if (isFirst)
                    {
                        isFirst = false;
                    }
                    else
                    {
                        nvBuilder.Append(",");
                        geoBuilder.Append(",");
                    }
                    nvBuilder.AppendFormat("{{name:\"{0}\",value:{1}}}", name, value / 100.0d);
                    geoBuilder.AppendFormat("\"{0}\":[{1},{2}]", name, lnglatArr[0], lnglatArr[1]);
                }
                else
                {
                    Console.WriteLine(string.Format("{0}格式不正确", lnglat));
                }
            }
            nvBuilder.Append("];");
            geoBuilder.Append("};");

            System.IO.File.WriteAllText(string.Format("{0}\\data.txt", AppDomain.CurrentDomain.BaseDirectory), nvBuilder.Append(geoBuilder.ToString()).ToString());
        }
    }
}
